package com.revengemission.sso.oauth2.server.service.impl;/**
 * 描述:
 * 包名:com.revengemission.sso.oauth2.server.service.impl
 * 版本信息: 版本1.0
 * 日期:2019/7/26
 * Copyright XXXXXX科技有限公司
 */


import com.revengemission.sso.oauth2.server.ro.Oauth2ClientRo;
import com.revengemission.sso.oauth2.server.service.Oauth2RedisService;
import com.revengemission.sso.oauth2.server.vo.AlreadyExistsException;
import com.revengemission.sso.oauth2.server.vo.EntityNotFoundException;
import com.revengemission.sso.oauth2.server.vo.JsonObjects;
import com.revengemission.sso.oauth2.server.vo.OauthClient;
import com.revengemission.sso.oauth2.server.persistence.entity.OauthClientEntity;
import com.revengemission.sso.oauth2.server.service.OauthClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @describe：
 * @author: lvmoney/XXXXXX科技有限公司
 * @version:v1.0 2019/7/26 16:35
 */
@Service
public class RedisOauthClientServiceImpl implements OauthClientService {
    @Autowired
    Oauth2RedisService oauth2RedisService;

    @Override
    public OauthClient findByClientId(String clientId) throws EntityNotFoundException {
        Oauth2ClientRo oauth2ClientRo = new Oauth2ClientRo();
        Map<String, OauthClient> data = new HashMap<>();
        OauthClient iOauthClient = new OauthClient();
        iOauthClient.setClientId("SampleClientId");
        iOauthClient.setClientSecret("$2a$10$gcrWom7ubcRaVD1.6ZIrIeJP0mtPLH5J9V/.8Qth59lZ4B/5HMq96");
        iOauthClient.setScope("user_info");
        iOauthClient.setAuthorizedGrantTypes("authorization_code,refresh_token,password");
        iOauthClient.setWebServerRedirectUri("http://localhost:10380/login/oauth2/code/sso-login");
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        iOauthClient.setAuthorities("ROLE_TRUSTED_CLIENT");
        iOauthClient.setAccessTokenValidity(3600);
        iOauthClient.setRefreshTokenValidity(2592000);
        iOauthClient.setId("1");
        iOauthClient.setApplicationName("SampleClinetId 测试应用");
        iOauthClient.setRecordStatus(0);
        iOauthClient.setVersion(0);
        data.put("SampleClientId", iOauthClient);
        oauth2ClientRo.setData(data);
        oauth2ClientRo.setExpire(180000l);
        oauth2RedisService.client2Redis(oauth2ClientRo);
        OauthClient oauthClient = oauth2RedisService.getClientByClientId(clientId);
        if (oauthClient == null) {
            throw new EntityNotFoundException(clientId + " not found!");
        }
        return oauthClient;
    }

    @Override
    public JsonObjects<OauthClient> list(int pageNum, int pageSize, String sortField, String sortOrder) {
        return null;
    }

    @Override
    @Transactional
    public OauthClient create(OauthClient oauthClient) throws AlreadyExistsException {
        return null;
    }

    @Override
    public OauthClient retrieveById(long id) throws EntityNotFoundException {
        return null;
    }

    @Override
    @Transactional
    public OauthClient updateById(OauthClient oauthClient) throws EntityNotFoundException {
        return null;
    }

    @Override
    @Transactional
    public void updateRecordStatus(long id, int recordStatus) {

    }
}
